package com.jinlejia.bbs.ui.mine.others;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.jinlejia.bbs.R;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.databinding.BbsActivityOtherUserMainBinding;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.TakeAttentionLogic;
import com.jinlejia.bbs.logic.UserInfoLogic;
import com.jinlejia.bbs.ui.mine.BBSAttentionActivity;
import com.jinlejia.bbs.ui.mine.BBSMyFansActivity;

import java.util.ArrayList;
import java.util.List;

public class BBSOtherUserMainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener,OnNotifyResultListener {
    private BbsActivityOtherUserMainBinding binding;
    private UserInfoLogic logic;
    private String pin;
    private boolean canToUserInfo;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.bbs_activity_other_user_main);
        binding.setCommonEvent(CommonEvent.obtain());
        binding.setEvent(this);
        this.context = this;
        pin = getIntent().getStringExtra("pin");
        canToUserInfo = getIntent().getBooleanExtra("toUser",true);
        if (logic==null){
            logic = new UserInfoLogic(this);
        }
        binding.swipeRefreshLayout.setOnRefreshListener(this);

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.swipeRefreshLayout.setRefreshing(true);
                refresh();
            }
        });
    }

    private float dp2px(float value){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,getResources().getDisplayMetrics());
    }

    private int notifyCount = 0;
    @Override
    public void onNotify() {
        notifyCount = notifyCount +1;
        if (notifyCount==3){
            binding.swipeRefreshLayout.setRefreshing(false);
        }
    }

    private int loadCount = 0;
    @Override
    public void onNotifyLoadMore() {
        loadCount = loadCount +1;
        binding.swipeRefreshLayout.setRefreshing(true);
    }
    @Override
    public void onNotifyLoadResult(){
        loadCount = loadCount -1;
        if (loadCount==0){
            binding.swipeRefreshLayout.setRefreshing(false);
        }
    }

    private void refresh(){
        notifyCount = 0;
        logic.loadInfo(pin, new UserInfoLogic.InfoLogicCallBack() {
            @Override
            public void onResult(BBSUserBean userBean) {
                onNotify();
                binding.setUser(userBean);
                Glide.with(context)
                        .load(userBean.getUserAvatar())
                        .dontAnimate()
                        .placeholder(R.mipmap.bbs_user_no_avatar)
                        .fallback(R.mipmap.bbs_user_no_avatar)
                        .into(new SimpleTarget<Drawable>() {
                            @Override
                            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                binding.circleImageView.setImageDrawable(resource); //显示图片
                            }
                        });

                if (!userBean.isSelf()){
                    binding.likeTextView.setVisibility(View.VISIBLE);
                    int type = userBean.getAttentionType();
                    changeAttention(type);
                }

            }
        });
        OnRefreshLoadListener questionListener = (OnRefreshLoadListener) questionFragment;
        OnRefreshLoadListener askListener = (OnRefreshLoadListener) askFragment;
        questionListener.onRefresh();
        askListener.onRefresh();
    }


    private Fragment questionFragment;
    private Fragment askFragment;
    private String[] mTitles = {"TA的提问", "TA的回答"};
    private void initView() {
        List<Fragment> mFragments= new ArrayList<>();
        questionFragment = new BBSOtherQuestionFragment();
        Bundle args = new Bundle();
        args.putString("pin",pin);
        args.putBoolean("toUser",canToUserInfo);
        questionFragment.setArguments(args);
        mFragments.add(0, questionFragment);
        askFragment = new BBSOtherAskFragment();
        askFragment.setArguments(new Bundle(args));
        mFragments.add(1, askFragment);
        ViewPagerAdapter mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), mTitles, mFragments);
        binding.viewpager.setAdapter(mViewPagerAdapter);
        binding.viewpager.setOffscreenPageLimit(mTitles.length);
        binding.tabLayout.setTabsFromPagerAdapter(mViewPagerAdapter);
        binding.tabLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.tabLayout.setupWithViewPager(binding.viewpager);
            }
        });
        binding.likeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attention();
            }
        });
    }

    private void changeAttention(int type){
        System.out.println("------------->改变了("+type+")");
        final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) binding.likeTextView.getLayoutParams();
        switch (type){
            case 0: //显示关注
                binding.likeTextView.setText("关注");
                binding.likeTextView.setBackgroundResource(R.drawable.bbs_attention_yellow);
                params.rightMargin = -(int) dp2px(30);
                binding.likeTextView.setLayoutParams(params);
                break;
            case 1: //显示已关注
                binding.likeTextView.setText("已关注");
                binding.likeTextView.setBackgroundResource(R.drawable.bbs_attention_gary);
                params.rightMargin = -(int) dp2px(30);
                binding.likeTextView.setLayoutParams(params);
                break;
            case 2: //互相关注
                binding.likeTextView.setBackgroundResource(R.drawable.bbs_attention_gary);
                binding.likeTextView.setText("互相关注");
                params.rightMargin = -(int) dp2px(10);
                binding.likeTextView.setLayoutParams(params);
                break;
        }
        binding.likeTextView.invalidate();
    }

    //关注
    private TakeAttentionLogic takeAttentionLogic;
    private void attention(){
        if (takeAttentionLogic==null){
            takeAttentionLogic = new TakeAttentionLogic(this);
        }
        takeAttentionLogic.execute(pin, new TakeAttentionLogic.LogicCallBack() {
            @Override
            public void onResult(int result) {
                changeAttention(result);
            }
        });
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onRefresh() {
        refresh();
    }

    //跳转至他的关注
    public void toAttentionCountList(View view){
        Intent intent = new Intent();
        intent.setClass(this,BBSAttentionActivity.class);
        intent.putExtra("pin",pin);
        startActivity(intent);
    }

    //跳转至他的粉丝
    public void toFansActivity(View view){
        Intent intent = new Intent();
        intent.setClass(this,BBSMyFansActivity.class);
        intent.putExtra("pin",pin);
        startActivity(intent);
    }
    private static class ViewPagerAdapter extends FragmentStatePagerAdapter {

        private String[] mTitles;
        private List<Fragment> mFragments;

        ViewPagerAdapter(FragmentManager fm, String[] mTitles, List<Fragment> mFragments) {
            super(fm);
            this.mTitles = mTitles;
            this.mFragments = mFragments;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
